<template>
  <el-card class="content">
    <el-row>
      <el-col :span="4">
        <span style="fontsize: 16px">尾矿库:</span>
        <el-select
          placeholder="请选择尾矿库"
          style="width: 200px"
          size="mini"
          v-model="eddform.tailingPondId"
          clearable
        >
          <el-option
            v-for="item in opations"
            :key="item.id"
            :label="item.tailingName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <span style="fontsize: 16px">参考系名:</span>
        <el-input
          size="mini"
          style="width: 200px"
          placeholder="请输入参考系名"
          v-model="eddform.referenceName"
          clearable
        >
        </el-input>
      </el-col>
      <el-col :span="5">
        <el-button class="black-button" size="small" @click="searchItem()"
          >查询</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="dialogVisible = true"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <p class="bg-purple">参考系信息列表</p>
    <el-table :data="table_data" stripe size="small">
      <el-table-column
        label="参考系名"
        prop="referenceName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="所属尾矿库"
        prop="tailingPondName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="修改时间"
        prop="updateTime"
        align="center"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
      ></el-table-column>
      <el-table-column
        label="备注"
        prop="remark"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="(isdialogVisible = true), handleEdit(scope.row)"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="delet(scope.row)"
            >删除</el-button
          >
          <!-- <el-button type="text" size="small">绑定传感器</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 15]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <el-dialog
      title="增加参考系"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="closeDialogVisible"
    >
      <div
        style="
          display: block;
          height: 75vh;
          overflow-y: auto;
          text-align: center;
        "
      >
        <img
          src="../../../assets/1.png"
          alt=""
          style="height: 300px; margin-left: 25px"
        />
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="110px"
          style="margin-top: 25px"
        >
          <el-form-item label="参考系名称" prop="referenceName">
            <el-input
              v-model="form.referenceName"
              placeholder="请输入参考系名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="尾矿库" prop="tailingPondId">
            <el-select
              placeholder="请选择尾矿库"
              style="width: 100%"
              v-model="form.tailingPondId"
            >
              <el-option
                v-for="item in opations"
                :key="item.id"
                :label="item.tailingName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="交点经度L(°)" prop="x1">
            <el-input
              v-model="form.x1"
              placeholder="请输入交点经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="交点纬度B(°)" prop="y1">
            <el-input
              v-model="form.y1"
              placeholder="请输入交点纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="水平经度L(°)" prop="x2">
            <el-input
              v-model="form.x2"
              placeholder="请输入水平经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="水平纬度B(°)" prop="y2">
            <el-input
              v-model="form.y2"
              placeholder="请输入水平纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="垂直经度L(°)" prop="x3">
            <el-input
              v-model="form.x3"
              placeholder="请输入垂直经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="垂直纬度B(°)" prop="y3">
            <el-input
              v-model="form.y3"
              placeholder="请输入垂直纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submit()">确 定</el-button>
          <el-button @click="closeDialogVisible()">取 消</el-button>
        </span>
      </div>
    </el-dialog>

    <el-dialog
      title="修改参考系"
      :visible.sync="isdialogVisible"
      width="30%"
      :before-close="closeisDialogVisible"
    >
      <div
        style="
          display: block;
          height: 75vh;
          overflow-y: auto;
          text-align: center;
        "
      >
        <img
          src="../../../assets/1.png"
          alt=""
          style="height: 300px; margin-left: 25px"
        />
        <el-form
          ref="addForm"
          :rules="rulesForm"
          :model="addForm"
          label-width="110px"
          style="margin-top: 25px"
        >
          <el-form-item label="参考系名称" prop="referenceName">
            <el-input
              v-model="addForm.referenceName"
              placeholder="请输入参考系名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="尾矿库" prop="tailingPondId">
            <el-select
              placeholder="请选择尾矿库"
              style="width: 100%"
              v-model="addForm.tailingPondId"
            >
              <el-option
                v-for="item in opations"
                :key="item.id"
                :label="item.tailingName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="交点经度L(°)" prop="x1">
            <el-input
              v-model="addForm.x1"
              placeholder="请输入交点经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="交点纬度B(°)" prop="y1">
            <el-input
              v-model="addForm.y1"
              placeholder="请输入交点纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="水平经度L(°)" prop="x2">
            <el-input
              v-model="addForm.x2"
              placeholder="请输入水平经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="水平纬度B(°)" prop="y2">
            <el-input
              v-model="addForm.y2"
              placeholder="请输入水平纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="垂直经度L(°)" prop="x3">
            <el-input
              v-model="addForm.x3"
              placeholder="请输入垂直经度L(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="垂直纬度B(°)" prop="y3">
            <el-input
              v-model="addForm.y3"
              placeholder="请输入垂直纬度B(°)"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="addForm.remark"
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addubmit()">确 定</el-button>
          <el-button @click="closeisDialogVisible()">取 消</el-button>
        </span>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getReferenceListByPage,
  getTailingPondList,
  insertReferenceFrame,
  updateReferenceFrame,
  deleteReferenceFrame,
} from "../../../api/cankaoxi";
export default {
  name: "FrameManagementChild",
  data() {
    return {
      total: null,
      query: {
        page: 1,
        pagesize: 10,
      },
      eddform: {
        referenceName: "",
        tailingPondId: "",
      },
      dialogVisible: false,
      isdialogVisible: false,
      addForm: {
        id: "",
        referenceName: "",
        remark: "",
        tailingPondId: "",
        unitId: window.sessionStorage.getItem("unitId"),
        x1: "",
        y1: "",
        x2: "",
        y2: "",
        x3: "",
        y3: "",
      },
      form: {
        referenceName: "",
        remark: "",
        tailingPondId: "",
        unitId: window.sessionStorage.getItem("unitId"),
        x1: "",
        y1: "",
        x2: "",
        y2: "",
        x3: "",
        y3: "",
      },
      table_data: [],
      unitId: "",
      opations: "",
      rules: {
        referenceName: [{ required: true, message: "请输入", trigger: "blur" }],
        remark: [{ required: true, message: "请输入", trigger: "change" }],
        tailingPondId: [
          { required: true, message: "请输入", trigger: "change" },
        ],
        x1: [{ required: true, message: "请输入", trigger: "change" }],
        y1: [{ required: true, message: "请输入", trigger: "change" }],
        x2: [{ required: true, message: "请输入", trigger: "change" }],
        y2: [{ required: true, message: "请输入", trigger: "change" }],
        x3: [{ required: true, message: "请输入", trigger: "change" }],
        y3: [{ required: true, message: "请输入", trigger: "change" }],
      },
      rulesForm: {
        referenceName: [{ required: true, message: "请输入", trigger: "blur" }],
        remark: [{ required: true, message: "请输入", trigger: "change" }],
        tailingPondId: [
          { required: true, message: "请输入", trigger: "change" },
        ],
        x1: [{ required: true, message: "请输入", trigger: "change" }],
        y1: [{ required: true, message: "请输入", trigger: "change" }],
        x2: [{ required: true, message: "请输入", trigger: "change" }],
        y2: [{ required: true, message: "请输入", trigger: "change" }],
        x3: [{ required: true, message: "请输入", trigger: "change" }],
        y3: [{ required: true, message: "请输入", trigger: "change" }],
      },
    };
  },
  created() {
    this.unitId = window.sessionStorage.getItem("unitId");
    this.getlist();
    this.userList();
  },
  methods: {
    handleSizeChange(val) {
      this.query.pagesize = val;
      // this.getStaionListPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      // this.getStaionListPage();
    },
    //尾矿库
    async userList() {
      await getTailingPondList({
        unitId: this.unitId,
      }).then((res) => {
        this.opations = res.parameter;
        console.log(res);
      });
    },
    //表格
    async getlist() {
      await getReferenceListByPage({
        unitId: this.unitId,
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
      }).then((res) => {
        if (res.resultCode == 200) {
          this.total = res.parameter.total;
          this.table_data = res.parameter.list;
          console.log(this.table_data);
        } else {
          this.table_data = [];
        }
      });
    },
    //查询
    async searchItem() {
      await getReferenceListByPage({
        unitId: this.unitId,
        referenceName: this.eddform.referenceName,
        tailingPondId: this.eddform.tailingPondId,
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
      }).then((res) => {
        if (res.resultCode == 200) {
          // this.$message({
          //   message: "查询成功",
          //   type: "success",
          //   offset: 450,
          //   duration: 1000,
          // });
          this.total = res.parameter.total;
          this.table_data = res.parameter.list;
        } else {
          this.$message({
            message: "数据为空",
            offset: 450,
            duration: 1000,
          });
          this.table_data = [];
        }
      });
    },
    //新增
    async submit() {
      this.$refs.form.validate(async (val) => {
        if (val) {
          await insertReferenceFrame({
            referenceName: this.form.referenceName,
            remark: this.form.remark,
            tailingPondId: this.form.tailingPondId,
            unitId: this.form.unitId,
            x1: this.form.x1,
            y1: this.form.y1,
            x2: this.form.x2,
            y2: this.form.y2,
            x3: this.form.x3,
            y3: this.form.y3,
          }).then((res) => {
            if (res.resultCode == 200) {
              this.$message({
                message: "新增成功",
                type: "success",
                offset: 450,
                duration: 1000,
              });

              this.getlist();
            } else {
              this.$message({
                message: "新增失败",
                type: "error",
                offset: 450,
                duration: 1000,
              });
            }
          });
          this.dialogVisible = false;
          this.$refs.form.resetFields();
        } else {
          this.$message({
            message: "请完善表单信息",
            type: "error",
            offset: 450,
            duration: 1000,
          });
        }
      });
    },
    //添加取消
    closeDialogVisible() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },

    //添加取消(修改)
    closeisDialogVisible() {
      this.isdialogVisible = false;
      this.$refs.addForm.resetFields();
    },
    //修改
    handleEdit(row) {
      this.addForm.referenceName = row.referenceName;
      this.addForm.remark = row.remark;
      this.addForm.tailingPondId = row.tailingPondId;
      this.addForm.x1 = row.x1;
      this.addForm.y1 = row.y1;
      this.addForm.x2 = row.x2;
      this.addForm.y2 = row.y2;
      this.addForm.x3 = row.x3;
      this.addForm.y3 = row.y3;
      this.addForm.id = row.id;
    },
    //新增(修改)
    async addubmit() {
      this.$refs.addForm.validate(async (val) => {
        if (val) {
          await updateReferenceFrame({
            id: this.addForm.id,
            referenceName: this.addForm.referenceName,
            remark: this.addForm.remark,
            tailingPondId: this.addForm.tailingPondId,
            unitId: this.addForm.unitId,
            x1: this.addForm.x1,
            y1: this.addForm.y1,
            x2: this.addForm.x2,
            y2: this.addForm.y2,
            x3: this.addForm.x3,
            y3: this.addForm.y3,
          }).then((res) => {
            if (res.resultCode == 200) {
              this.$message({
                message: "修改成功",
                type: "success",
                offset: 450,
                duration: 1000,
              });
              this.getlist();
            } else {
              this.$message({
                message: "修改失败",
                type: "error",
                offset: 450,
                duration: 1000,
              });
            }
          });
          this.isdialogVisible = false;
        } else {
          this.$message({
            message: "请完善表单信息",
            type: "error",
            offset: 450,
            duration: 1000,
          });
        }
      });
    },
    //删除
    async delet(row) {
      const confirmResult = await this.$confirm(
        "此操作将强制永久删除该数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message({
          message: "已取消删除",
          type: "info",
          offset: 450,
          duration: 1000,
        });
      }
      await deleteReferenceFrame({
        referenceId: row.id,
      }).then((res) => {
        if (res.resultCode == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
            offset: 450,
            duration: 1000,
          });
          this.getlist();
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
            offset: 450,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>   

<style scoped>
.content {
  height: 87vh;
  overflow: auto;
}

.black-button {
  background-color: black;
  color: white;
}

.bg-purple {
  background: #d3dce6;
  height: 30px;
  font-size: 14px;
  line-height: 30px;
  padding-left: 10px;
}
.el-button {
  font-size: 14px;
  margin-bottom: 8px;
}
.el-table {
  display: block;
  height: 67.2vh;
  overflow-y: auto;
  margin-bottom: 15px;
}
.el-table::before {
  z-index: inherit;
}
</style>
